package com.sz;

import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import com.sz.config.RibbonConfig;
import com.sz.job.MyAdaptableJobFactory;
import com.sz.job.MyJob;
import com.sz.utils.CronUtils;
import com.sz.utils.DateUtils;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;

@SpringBootApplication
@EnableDiscoveryClient   //启用注册客户端；
@EnableFeignClients       //启用微服务调用；
@EnableDistributedTransaction       //分布式事务
@MapperScan("com.sz.mapper")
@ImportResource("classpath:*.xml")
@RibbonClient(name = "RibbonConfig", configuration = RibbonConfig.class)    //负载均衡
public class TransferStarters {

    public static void main(String[] args) {
        //程序入口
        System.out.println("-------------ccc---------------");
        SpringApplication.run(TransferStarters.class,args);
    }

    //自动装载工厂
    @Bean(name = "jobFactory")
    public MyAdaptableJobFactory myAdaptableJobFactory(){
        return new MyAdaptableJobFactory();
    }

    //任务
    @Bean(name = "jobDetail")
    public JobDetailFactoryBean jobDetailFactoryBean() {// ScheduleTask为需要执行的任务
        JobDetailFactoryBean jobDetail = new JobDetailFactoryBean();
        jobDetail.setName("job1");// 设置任务的名字
        jobDetail.setGroup("myJobGroup1");// 设置任务的分组，这些属性都可以存储在数据库中，在多任务的时候使用
        jobDetail.setJobClass(MyJob.class);
        return jobDetail;
    }

    //触发器
    @Bean(name = "cronJobTrigger")
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetail){
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(jobDetail.getObject());
        trigger.setStartDelay(0L);
        //trigger.setCronExpression("0 0/1 * 1/1 * ? *"); //每分钟执行一次的cron定时任务
        //控制到账时间
        //一天
        //String cron = CronUtils.getCron(DateUtils.getSpecifiedDayAfter());
        //30秒
        String cron = CronUtils.getCron(DateUtils.getMyDate());
        System.out.println("当前时间"+DateUtils.getDateString());

        trigger.setCronExpression("0 0/20 * * * ? "); //每分钟执行一次的cron定时任务
        trigger.setName("cron-trigger");// trigger的name
        return trigger;
    }

    @Autowired
    DataSource dataSource;

    //调度器
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(Trigger... triggers) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        bean.setOverwriteExistingJobs(false);
        // 延时启动，应用启动1秒后
        bean.setStartupDelay(1);
        // 注册触发器,
        bean.setTriggers(triggers);
        bean.setDataSource(dataSource);
        bean.setJobFactory(myAdaptableJobFactory());
        return bean;
    }

}
